﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using MRS.DBL.Model;
using MRSUtil;

namespace MRS.WebUI.Manager.Industry
{
    public partial class EditorIndustry : ManageBasePage
    {
        static log4net.ILog _logger = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        MRS.DBL.BLL.IndustryMgr _industryBLL = new DBL.BLL.IndustryMgr();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) 
            {
                int industryId = string.IsNullOrEmpty(Request.QueryString["id"]) ? 0 : Convert.ToInt32(Request.QueryString["id"]);
                if (industryId > 0) 
                {
                    ViewState["IsEditor"] = true;
                    BindIndustryInfo(industryId);
                }
            }
        }

        /// <summary>
        /// Bind Industry Model Infomation By IndustryId
        /// </summary>
        /// <param name="industryId"></param>
        private void BindIndustryInfo(int industryId) 
        {
            try
            {
                IndustryModel model = GetIndustryModel(industryId);
                if (model == null)
                    return;
                txtIndustryName.Text = model.IndustryName;
                if (MRSConsts.Code_Enable == model.Status)
                    rdoEnable.Checked = true;
                else
                    rdoDisabled.Checked = true;
            }
            catch (Exception ex)
            {
                _logger.ErrorFormat("Load Industry Id {0} :{1}", industryId, ex);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            IndustryModel oIndustry = GetPageInputInfo();
            if (oIndustry == null)
                return;

            bool IsEditor = ViewState["IsEditor"] == null;//Check Current Operation Is Editor Model Or Not
            
            if (IsEditor)//Update Industry Model
            {
                int industryId = Convert.ToInt32(Request.QueryString["id"]);
                IndustryModel IndustryModel = GetIndustryModel(industryId);
                if (IndustryModel == null)
                {
                    Common.Util.JSHelper.Alert(this, "获取行业信息失败");
                    return;
                }
                IndustryModel.IndustryName = oIndustry.IndustryName;
                IndustryModel.Status = oIndustry.Status;

                UpdateIndustryInfo(IndustryModel);
            }
            else //Create New Industry Model
            {
                NewIndustryModel(oIndustry);
            }
        }

        /// <summary>
        /// New Industry Model
        /// </summary>
        /// <param name="oNewModel"></param>
        /// <returns></returns>
        private void NewIndustryModel(IndustryModel oNewModel)
        {
            int newIndustryId = _industryBLL.AddIndustry(oNewModel);
            if (MRSConsts.Code_Exists == newIndustryId)//Exists
            {
                _logger.InfoFormat("CurrentLoginUser : {0} Create New Industry Model Infomation Failed : “{1}” have been Exists in DataBase", base.CurLoginUser.Id, oNewModel.IndustryName);
                Common.Util.JSHelper.Alert(this, "行业名称已存在");
            }
            else if (newIndustryId > 0)//Success
            {
                _logger.InfoFormat("CurrentLoginUser : {0} Create New Industry : {1} Success", base.CurLoginUser.Id, oNewModel.IndustryName);
                Common.Util.JSHelper.Alert(this, "添加新行业信息成功");
            }
            else
            {
                _logger.ErrorFormat("CurrentLoginUser : {0} Create New Industry Model Infomation Failed: System Exception", base.CurLoginUser.Id);
                Common.Util.JSHelper.Alert(this, "添加新行业信息失败");
            }
        }

        /// <summary>
        /// Update Industry Infomation
        /// </summary>
        /// <param name="model"></param>
        private void UpdateIndustryInfo(IndustryModel model) 
        {
            int updateReuslt = _industryBLL.UpdateIndustry(model);
            if (MRSConsts.Code_Exists==updateReuslt)
            {
                _logger.InfoFormat("CurrentLoginUser : {0} Update Industry Model Infomation Failed The Industry Name Existed", base.CurLoginUser.Id);
                Common.Util.JSHelper.Alert(this, "行业名称已存在");
            }
            else if (MRSConsts.Code_Success == updateReuslt) 
            {
                _logger.InfoFormat("CurrentLoginUser : {0} Update Id {1} Industry Name “{2}” Infomation  Success", base.CurLoginUser.Id, model.Id, model.IndustryName);
                Common.Util.JSHelper.Alert(this, "更新行业信息成功");
            }
            else
            {
                _logger.InfoFormat("CurrentLoginUser : {0} Update Industry Model Infomation Failed", base.CurLoginUser.Id);
                Common.Util.JSHelper.Alert(this, "更新行业信息失败");
            }
        }

        /// <summary>
        /// Get Industry Model Infomation By IndustryId
        /// </summary>
        /// <param name="industryId"></param>
        /// <returns></returns>
        private IndustryModel GetIndustryModel(int industryId) 
        {
            IndustryModel model = _industryBLL.GetIndustryModel(industryId);
            if (model == null)
            {
                _logger.InfoFormat("Get Industry Id {0} Model is Null", industryId);
                return null;
            }
            return model;
        }


        /// <summary>
        /// Get Industry Infomation From Page
        /// </summary>
        /// <returns>IndustryModel</returns>
        private IndustryModel GetPageInputInfo()
        {
            if(string.IsNullOrWhiteSpace(txtIndustryName.Text.Trim()))
            {
                Common.Util.JSHelper.Alert(this, "请输入行业名称");
                return null;
            }
            
            IndustryModel model = new IndustryModel();
            model.IndustryName = txtIndustryName.Text.Trim();
            if (rdoEnable.Checked)
                model.Status = MRSConsts.Code_Enable;
            else
                model.Status = MRSConsts.Code_Disabled;     
            model.CreateUserId = base.CurLoginUser.Id;      //Create By User Id

            return model;
        }
    }
}